package com.itheima.service.Impl;

import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.UserService;
import com.itheima.utils.MapperUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * @param
 * @return
 */
public class UserServiceImpl implements UserService {
    @Override
    public List<User> findAll() {
        //定义返回值
        List<User> list=null;
        //工具类操作走起
        SqlSession session = MapperUtil.getSession();
        /*获取接口代理对象*/
        UserMapper mapper = session.getMapper(UserMapper.class);
        //调用接口中的方法
        list=mapper.findAll();
        return list;
    }

    @Override
    public boolean save(User user) {
        //定义返回值
     boolean flag=false;
        //工具类操作走起
        SqlSession session = MapperUtil.getSession();
        /*获取接口代理对象*/
        UserMapper mapper = session.getMapper(UserMapper.class);
        //调用接口中的方法
        int i = mapper.save(user);
        if(i>0){
            flag=true;
        }
        return flag;
    }

    @Override
    public User findById(int id) {
        //定义返回值
        User user=null;
        //工具类操作走起
        SqlSession session = MapperUtil.getSession();
        /*获取接口代理对象*/
        UserMapper mapper = session.getMapper(UserMapper.class);
        //调用接口中的方法
        user = mapper.findById(id);
        return user;
    }

    @Override
    public boolean update(User user) {
        //定义返回值
        boolean flag=false;
        //工具类操作走起
        SqlSession session = MapperUtil.getSession();
        /*获取接口代理对象*/
        UserMapper mapper = session.getMapper(UserMapper.class);
        //调用接口中的方法
        int i = mapper.update(user);
        if(i>0){
            flag=true;
        }
        return flag;
    }

    @Override
    public boolean deleteByIds(String ids) {
        //定义返回值
        boolean flag=false;
        //在这里处理字符串
        String[] split = ids.split(",");
        //创建集合
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            list.add(Integer.parseInt(split[i]));
        }
        SqlSession session = MapperUtil.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        int i =mapper.deleteByIds(list);
        if(i>0){
            flag=true;
        }
        return flag;
    }
}
